package com.hkd.blogsystem.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.hkd.blogsystem.enums.ResultCodeEnum;
import com.hkd.blogsystem.model.CaptchaProperties;
import com.hkd.blogsystem.model.Result;
import com.hkd.blogsystem.model.UserInfo;
import com.hkd.blogsystem.service.UserService;
import com.hkd.blogsystem.utils.JwtUtils;
import com.hkd.blogsystem.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Result login(String username, String password){
        log.info("用户登录, userName: {}", username);
        if(!StringUtils.hasLength(username) && !StringUtils.hasLength(password)){
            return Result.fail(ResultCodeEnum.PARAM_INVLID.getCode(),"账号密码不能为空号");
        }
        UserInfo userInfo = userService.selectUserInfoByName(username);
        if(userInfo == null || userInfo.getId() == null){
            return Result.fail(ResultCodeEnum.PARAM_INVLID.getCode(),"用户不存在");
        }
        if(!SecurityUtils.verify(password,userInfo.getPassword())){
            log.error("密码验证失败");
            return Result.fail(ResultCodeEnum.PARAM_INVLID.getCode(),"用户密码错误");
        }

        //密码正确
        Map<String,Object> claims = new HashMap<String,Object>();
        claims.put("id",userInfo.getId());
        claims.put("name",userInfo.getUserName());

        String jwt = JwtUtils.genJwt(claims);
        return Result.success(jwt);
    }

    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request){
        String userToken = request.getHeader("user_token_header");
        //从token中获取用户ID
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        Assert.notNull(userId, "用户未登录");
        UserInfo userInfo = userService.selectUserInfoById(userId);
        return userInfo;
    }

    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId){
        Assert.notNull(blogId, "博客ID不能为null");
        return userService.selectAuthorInfoByBlogId(blogId);
    }

    @RequestMapping("/register")
    public Result addUserInfo(String username,String password){
        if(!StringUtils.hasLength(username) && !StringUtils.hasLength(password)){
            return Result.fail(ResultCodeEnum.PARAM_INVLID.getCode(),"账号密码不能为空号");
        }
        log.info("用户："+username+" 正在注册");
        Assert.notNull(username,"注册用户账号不能为空");
        UserInfo hasUser = userService.selectUserInfoByName(username);
        if(hasUser != null){
            log.error("用户名已被注册，换一吧");
            return Result.fail("用户名已被注册，换一吧");
        }
        Assert.notNull(password,"注册用户密码不能为空");
        Integer result = userService.registeerUserInfo(username,password);
        return result >= 1? Result.success("true"): Result.fail("注册失败");
    }


}
